
FreeTexMode = {
	CreateUnit = function ()
		local TexUI = cDXUIBase:New()
		TexUI:SetMovable(true)
		TexUI:SetVisible(false)
		cWindow:AddDXUI(TexUI)
		FreeTexMode.TexUI = TexUI
		FreeTexMode.CurFuncSeq = FreeTexMode.FuncSeq.Pass1
	end,
	FuncSeq = {
	Pass1 = function ()
		local EditObj = FreeTexMode.EditObj
		if not EditObj then
			return
		end
		local TexName = EditObj:GetTexture()
		TRACE(string.format("FreeTexMode - Texname (%s)\n", TexName))
		local Tex = g_MgrTex:Find(TexName)
		if not Tex then
			TRACE("FreeTexMode - No Texture applied\n")
			g_CurMode = ChangeMode_BackMode
			return
		end
		local twidth = Tex:GetWidth()
		local theight = Tex:GetHeight()

		local TexUI = FreeTexMode.TexUI
		TexUI:SetTexture(Tex)
		TexUI:SetPos(300,80)
		TexUI:SetSize(512,512*theight/twidth)
		TexUI:SetVisible(true)

		TexUI:SetOnMouseWheel(function (wheel)
			local UIwidth = TexUI:GetWidth()
			local UIheight = TexUI:GetHeight()
			local aspect = UIheight/UIwidth
			local step = 10
			if wheel>0 then
				TexUI:SetSize(UIwidth-10,UIheight-10*aspect)
			elseif wheel<0 then
				TexUI:SetSize(UIwidth+10,UIheight+10*aspect)
			end
		end)

		UILayout.Create(FreeTexConfirm)
		FreeTexConfirm.__SelfUI:NoCloseButton()
		FreeTexConfirm.__SelfUI:SetPos(800,100)
		FreeTexConfirm.Quit:SetOnClick(function ()
			FreeTexMode.FuncSeq.End()
			FreeTexMode.TexUI:SetVisible(false)
			FreeTexConfirm.__SelfUI:Close()
		end)
		FreeTexConfirm.Gen:SetOnClick(FreeTexMode.FuncSeq.Pass2)
		FreeTexConfirm.SetGrid:SetOnClick(function ()
			local left = tonumber(FreeTexConfirm.LeftEdit:GetText())
			local top  = tonumber(FreeTexConfirm.TopEdit:GetText())
			local right = tonumber(FreeTexConfirm.RightEdit:GetText())
			local bottom = tonumber(FreeTexConfirm.BottomEdit:GetText())
			if not left or not top or not right or not bottom then
				TRACE("FreeTexConfirm - SetGrid error Params!\n")
				return
			end
			TRACE(string.format("FreeTexConfirm - LT(%d,%d)->RB(%d,%d)\n",left,top,right,bottom))
			if twidth<=0 or theight<=0 then
				TRACE("FreeTexConfirm - SetGrid error texParams!\n")
				return
			end
			TexUI:SetTexcoordLT(left/twidth,top/theight)
			TexUI:SetTexcoordRB(right/twidth,bottom/theight)
			TexUI:SetSize(right-left,bottom-top)
			FreeTexMode.Left = left
			FreeTexMode.Top = top
			FreeTexMode.Right = right
			FreeTexMode.Bottom = bottom
		end)

		FreeTexMode.TexWidth = twidth
		FreeTexMode.TexHeight = theight
		FreeTexMode.Left = 0
		FreeTexMode.Top = 0
		FreeTexMode.Right = twidth
		FreeTexMode.Bottom = theight
		FreeTexMode.CurFuncSeq = IdleFunc
		g_CurMode = ChangeMode_BackMode
	end,
	Pass2 = function ()
		local TexUI = FreeTexMode.TexUI
		local EditObj = FreeTexMode.EditObj
		local BelongObj = EditObj.BelongObj

		if not EditObj.Vertices then
			TRACE("FreeTexMode - No Vertices !\n")
			return
		end
		local MeshName = BelongObj:GetMesh()
		local Mesh = g_MgrMesh:Find(MeshName)
		if not Mesh then
			TRACE("FreeTexMode - No Mesh!\n")
			return
		end

		local UIx = TexUI:GetX()
		local UIy = TexUI:GetY()
		local UIwidth = TexUI:GetWidth()
		local UIheight = TexUI:GetHeight()
		local GridWidth = FreeTexMode.Right - FreeTexMode.Left
		local GridHeight = FreeTexMode.Bottom - FreeTexMode.Top--]]

		BelongObj:CalObjSpaceTransform()

		TRACE("FreeTexMode - Generating Texcoords\n")
		Mesh:LockVertex()
		for _,v in ipairs(EditObj.Vertices) do
			local Pos = Mesh:GetPos(v)
			Pos = BelongObj:TransformToWorldSpace(Pos)
			Pos = cWindow:TransformToClientPos(Pos,g_Camera)
			Pos.x = ((Pos.x - UIx) % UIwidth / UIwidth * GridWidth + FreeTexMode.Left) / FreeTexMode.TexWidth
			Pos.y = ((Pos.y - UIy) % UIheight / UIheight * GridHeight + FreeTexMode.Top) / FreeTexMode.TexHeight
			Mesh:SetTexcoord(v,Pos)
		end
		Mesh:UnlockVertex()
		TRACE("FreeTexMode - Done\n")
	end,
	End = function ()
		FreeTexMode.EditObj = nil
		FreeTexMode.CurFuncSeq = FreeTexMode.FuncSeq.Pass1
	end
	}
}
